#!/usr/bin/env python
# -*- coding: utf-8 -*-


import simplekml
import os.path
from filecache.filecache_manager import *

from metlib.shell.fileutil import MKDIR


kmz_manager = FileCacheManager('kmz_img_tmp')

def gen_src_img_kmz(kmz_path, lon1=None, lat1=None, lon2=None, lat2=None, src_img_path=None,
                    colorbar_img_path=None, logo_img_path=None,
                    folder_name=gen_uuid32):
    """
    :param kmz_path: kmz stored path
    :param lon1:
    :param lat1:
    :param lat2:
    :param lon2:
    :param logo_img_path: logo img path
    :param src_img_path: src img path
    :param colorbar_img_path: colorbar img path
    """
    kml = simplekml.Kml()
    img_href = {}
    for img_path in [logo_img_path, src_img_path, colorbar_img_path]:
        if img_path is None:
            continue
        if os.path.isfile(img_path):
            href = kml.addfile(img_path)
        elif img_path.startswith('http'):
            href = img_path
        else:
            href = None
        img_href.setdefault(img_path, href)

    if callable(folder_name):
        folder_name = folder_name()
    fol = kml.newfolder(name=folder_name)
    # 替换document标签为fol
    kml.document = fol
    # ground overlay
    ground = fol.newgroundoverlay(name='src_img')
    ground.icon.href = img_href.get(src_img_path)

    east = max(lon1, lon2)
    west = min(lon1, lon2)
    north = max(lat1, lat2)
    south = min(lat1, lat2)
    ground.latlonbox.east = east
    ground.latlonbox.west = west
    ground.latlonbox.north = north
    ground.latlonbox.south = south
    ground.color = '99ffffff'

    # screen overlay
    fraction = simplekml.Units.fraction
    pixel = simplekml.Units.pixel
    insetpixels = simplekml.Units.insetpixels

    # logo
    if logo_img_path is not None:
        logo = fol.newscreenoverlay(name='logo')
        logo.icon.href = img_href.get(logo_img_path)
        logo.overlayxy = simplekml.OverlayXY(x=0, y=1, xunits=fraction,
                                             yunits=fraction)
        logo.screenxy = simplekml.ScreenXY(x=15, y=15, xunits=pixel,
                                           yunits=insetpixels)
        logo.size = simplekml.Size(x=100, y=100, xunits=pixel,
                                        yunits=pixel)

    # color bar
    if colorbar_img_path is not None:
        color_bar = fol.newscreenoverlay(name='color_bar')
        color_bar.icon.href = img_href.get(colorbar_img_path)
        color_bar.overlayxy = simplekml.OverlayXY(x=0, y=0.5, xunits=fraction,
                                                  yunits=fraction)
        color_bar.screenxy = simplekml.ScreenXY(x=15, y=0.5, xunits=pixel,
                                                yunits=fraction)
        color_bar.size = simplekml.Size(x=100, y=100, xunits=pixel,
                                        yunits=pixel)
        color_bar.size = simplekml.Size(x=-1, y=-1, xunits=fraction,
                                   yunits=fraction)
    dirname = os.path.dirname(kmz_path)
    MKDIR(dirname)
    kml.savekmz(kmz_path)


if __name__ == '__main__':
    pass
